﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CPPEI.Coolzon.Theme.Fashion
{
    public delegate void MouseLeftButtonUpHandler(double x, double y);
    public delegate void MouseLeftButtonMoveHandler(double x,double y);
    public class Hook
    {
        public event MouseLeftButtonUpHandler MouseLeftButtonUp;
        public event MouseLeftButtonMoveHandler MouseLeftButtonMove;

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);
        //安装钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        //卸载钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        //调用下一个钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        public HookProc MyProcedure;
        //获取模块句柄
        IntPtr intp = GetModuleHandle(null);
        //定义钩子句柄
        public static int downHook = 0;
        public static int upHook = 0;
        public static int hHook = 0;
        //定义钩子类型
        public const int WM_MOUSE_LL = 14;
        public const int WM_MOUSEMOVE = 0x0200;
        public const int WM_LBUTTONDOWN = 0x0201;
        public const int WM_LBUTTONUP = 0x0202;
        public const int WM_MBUTTONDOWN = 0x0207;

        public void SetMouseHook()
        {
            if (hHook == 0)
            {
                MyProcedure = new HookProc(this.MouseHookProc);
                //这里挂节钩子
                hHook = SetWindowsHookEx(WM_MOUSE_LL, MyProcedure, intp, 0);
            }
            else
            {
                //这里卸载钩子
                UnLoadHook();
            }
        }
        public int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            POINT MyPOINT = (POINT)Marshal.PtrToStructure(lParam, typeof(POINT));
            if (wParam == WM_LBUTTONUP)
            {
                if (MouseLeftButtonUp != null)
                {
                    MouseLeftButtonUp(MyPOINT.x, MyPOINT.y);
                }
            }
            if (wParam == WM_MOUSEMOVE)
            {
                if (MouseLeftButtonMove != null)
                {
                    MouseLeftButtonMove(MyPOINT.x, MyPOINT.y);
                }
            }
            if (nCode < 0)
            {
                return CallNextHookEx((IntPtr)hHook, nCode, (IntPtr)wParam, lParam);
            }
            else
            {
                return CallNextHookEx((IntPtr)hHook, nCode, (IntPtr)wParam, lParam);
            }
        }

        public void UnLoadHook()
        {
            bool ret = UnhookWindowsHookEx(hHook);
            hHook = 0;
        }
        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }
    }
}
